package utils;

import model.request.*;
import model.response.*;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class BuildDataUtil {
    /**
     * 构造下单map
     * @param request
     * @return
     */
    public static Map<String, String> buildMap(WXPayRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("appid", request.getAppid());
        map.put("attach", request.getAttach());
        map.put("body", request.getBody());
        map.put("mch_id", request.getMchId());
        map.put("nonce_str", request.getNonceStr());
        map.put("notify_url", request.getNotifyUrl());
        map.put("out_trade_no", request.getOutTradeNo());
        map.put("spbill_create_ip", request.getSpbillCreateIp());
        map.put("time_expire", request.getTimeExpire());
        map.put("total_fee", String.valueOf(request.getTotalFee()));
        map.put("trade_type", request.getTradeType());
        map.put("sign", request.getSign());
        return map;
    }

    /**
     * 构造查询map
     * @param request
     * @return
     */
    public static Map<String,String> buildMap(WXOrderQueryRequest request){
        Map<String, String> map = new HashMap<>();
        map.put("appid", request.getAppid());
        map.put("mch_id", request.getMchId());
        map.put("nonce_str", request.getNonceStr());
        map.put("sign", request.getSign());
        map.put("sign_type", request.getSignType());
        map.put("out_trade_no", request.getOutTradeNo());
        return map;
    }

    /**
     * 构造关单参数
     * @param request
     * @return
     */
    public static Map<String,String> buildMap(WXPayCloseRequest request){
        Map<String, String> map = new HashMap<>();
        map.put("appid", request.getAppid());
        map.put("mch_id", request.getMchId());
        map.put("nonce_str", request.getNonceStr());
        map.put("sign", request.getSign());
        map.put("out_trade_no", request.getOutTradeNo());
        return map;
    }

    /**
     * 构造退款map
     * @param request
     * @return
     */
    public static Map<String, String> buildMap(WXPayRefundRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("appid", request.getAppid());
        map.put("mch_id", request.getMchId());
        map.put("nonce_str", request.getNonceStr());
        map.put("notify_url", request.getNotifyUrl());
        map.put("out_refund_no", request.getOutRefundNo());
        map.put("out_trade_no", request.getOutTradeNo());
        map.put("refund_fee", String.valueOf(request.getRefundFee()));
        map.put("total_fee", String.valueOf(request.getTotalFee()));
        return map;
    }

    /**
     * 构造退款查询
     * @param request
     * @return
     */
    public static Map<String, String> buildMap(WXRefundQueryRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("appid", request.getAppid());
        map.put("mch_id", request.getMchId());
        map.put("nonce_str", request.getNonceStr());
        map.put("out_refund_no", request.getOutRefundNo());
        return map;
    }


    /**
     * 构造获取沙箱signkey的map
     * @param request
     * @return
     */
    public static Map<String, String> buildMap(WXSignkeyRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("mch_id", request.getMchId());
        map.put("nonce_str", request.getNonceStr());
        return map;
    }

    public static Map<String, String> buildMap(WXPayResponse payResponse, String signKey){
        Calendar calendar = Calendar.getInstance();
        Map<String, String> map = new HashMap<>();
        map.put("appid", payResponse.getAppid());
        map.put("noncestr", payResponse.getNonceStr());
        map.put("package", "Sign=WXPay");
        map.put("partnerid", payResponse.getMchId());
        map.put("prepayid", payResponse.getPrepayId());
        map.put("timestamp", String.valueOf(System.currentTimeMillis()/1000L));
        map.put("sign", SignatureUtil.sign(map, signKey));
        return map;
    }

    /**
     * 支付预下单结果封装
     * @param response
     * @return
     */
    /*private PayResponse buildPrePayResponse(WXPayResponse response) {
        //跳转的url
        PayResponse payResponse = new PayResponse();
        payResponse.setCode(response.getResultCode());
        payResponse.setMsg(response.getReturnMsg());
        if(StringUtils.isNotBlank(response.getMwebUrl())){
            payResponse.setMwebUrl(response.getMwebUrl());
        }
        return payResponse;
    }*/

    /**
     * 查询结果封装
     * @param response
     * @return
     */
    public static QueryResponse buildQueryResponse(WXOrderQueryResponse response){
        QueryResponse queryResponse = new QueryResponse();
        queryResponse.setCashFee(response.getCashFee());
        queryResponse.setCouponFee(response.getCouponFee());
        queryResponse.setOpenid(response.getOpenid());
        queryResponse.setOrderId(response.getOutTradeNo());
        queryResponse.setTotal_fee(response.getTotalFee());
        queryResponse.setBankType(response.getBankType());
        queryResponse.setFeeType(response.getFeeType());
        queryResponse.setTradeState(response.getTradeState());
        return queryResponse;
    }

    /**
     * 退款结果封装
     * @param response
     * @return
     */
    public static RefundResponse buildRefundResponse(WXPayRefundResponse response) {
        RefundResponse refundResponse = new RefundResponse();
        refundResponse.setOrderId(response.getOutTradeNo());
        refundResponse.setOrderAmount(Long.valueOf(response.getTotalFee()));
        refundResponse.setOutTradeNo(response.getTransactionId());
        refundResponse.setRefundId(response.getOutRefundNo());
        refundResponse.setOutRefundNo(response.getRefundId());
        return refundResponse;
    }
}
